package com.cqkjxy.service;

import com.cqkjxy.commons.constants.ScoreDetial;
import com.cqkjxy.commons.services.AbstractService;
import com.cqkjxy.dao.ScoringDao;
import com.cqkjxy.entity.ScoringDetailEntity;
import com.cqkjxy.entity.StaffEntity;
import com.cqkjxy.entity.TechProjectEntity;
import com.cqkjxy.repositories.ScoringRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ScoringService extends AbstractService<ScoringDetailEntity, ScoringRepository, ScoringDao> {

    @Autowired
    private ScoringDao scoringDao;
    @Autowired
    private StaffService staffService;

    @Override
    protected ScoringDao getDao() {
        return scoringDao;
    }

    @Transactional(readOnly = false)
    public Iterable<ScoringDetailEntity> save(TechProjectEntity entity) {
        //分数容器
        List<ScoringDetailEntity> scores = new ArrayList<>();
        Set<StaffEntity> mainStaffEntities = entity.getMainStaffEntities();
        Set<StaffEntity> otherStaffEntities = entity.getOtherStaffEntities();
        List<StaffEntity> mainStaffs = new ArrayList<>(mainStaffEntities);
        List<StaffEntity> otherStaffs = new ArrayList<>(otherStaffEntities);
        Integer type = entity.getTechTypeEntity().getPkId();
        String itemRewardClass = entity.getItemRewardClass();
        String itemPublishClass = entity.getItemPublishClass();
        String itemReport = entity.getItemReport();
        String patentType = entity.getPatentType();
        switch (type) {
            /*合理化建议*/
            case 1:
                /*主要建议人员每人加两分*/
                scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.SUGGEST_SCORE_MAIN);

                switch (itemRewardClass) {
                    case "c1":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.SUGGEST_REWARD_CQ_FIRST_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.SUGGEST_REWARD_CQ_FIRST_OTHER);
                        break;
                    case "g1":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.SUGGEST_REWARD_CO_FIRST_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.SUGGEST_REWARD_CO_FIRST_OTHER);
                        break;
                    case "c2":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.SUGGEST_REWARD_CQ_SECOND_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.SUGGEST_REWARD_CQ_SECOND_OTHER);
                        break;
                    case "g2":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.SUGGEST_REWARD_CO_SECOND_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.SUGGEST_REWARD_CO_SECOND_OTHER);
                        break;
                    case "c3":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.SUGGEST_REWARD_CQ_THIRD_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.SUGGEST_REWARD_CQ_THIRD_OTHER);
                        break;
                    case "g3":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.SUGGEST_REWARD_CO_THIRD_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.SUGGEST_REWARD_CO_THIRD_OTHER);
                        break;
                    case "g4":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.SUGGEST_REWARD_CO_FOURTH_MAIN);
                        break;
                    case "g5":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.SUGGEST_REWARD_CO_FIFTH_MAIN);
                        break;
                    default:
                }
                break;
            /*PPT课件*/
            case 2:
                /*根据发布等级加分。*/
                switch (itemPublishClass) {
                    case "p1":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PPT_COMPANY_PUBLISH_SCORE_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PPT_COMPANY_PUBLISH_SCORE_OTHER);
                        break;
                    case "p2":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PPT_CENTER_PUBLISH_SCORE_MAIN);
                        break;
                    default:

                }
                /*根据奖励等级。*/
                switch (itemRewardClass) {
                    case "c1":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PPT_REWARD_CQ_FIRST_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PPT_REWARD_CQ_FIRST_OTHER);
                        break;
                    case "g1":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PPT_REWARD_CO_FIRST_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PPT_REWARD_CO_FIRST_OTHER);
                        break;
                    case "c2":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PPT_REWARD_CQ_SECOND_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PPT_REWARD_CQ_SECOND_OTHER);
                        break;
                    case "g2":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PPT_REWARD_CO_SECOND_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PPT_REWARD_CO_SECOND_OTHER);
                        break;
                    case "c3":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PPT_REWARD_CQ_THIRD_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PPT_REWARD_CQ_THIRD_OTHER);
                        break;
                    case "g3":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PPT_REWARD_CO_THIRD_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PPT_REWARD_CO_THIRD_OTHER);
                        break;
                    default:
                }
                break;
            /*技术论文*/
            case 3:
                /*投稿：第一作者加4分；其他作者加3分*/
                scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PAPER_MAIN_AUTHOR);
                scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PAPER_OTHER_AUTHOR);

                /*根据发表等级：
                p5核心：第一作者加10分，其他作者加3分；
                p3集团公司期刊：第一作者加5分，其他作者加2分
                p4公司期刊：第一作者加3分，其他作者加1分*/
                switch (itemPublishClass) {
                    case "p3":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PAPER_GENERAL_JOURNAL_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PAPER_GENERAL_JOURNAL_OTHER);
                        break;
                    case "p4":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PAPER_COMPANY_JOURNAL_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PAPER_COMPANY_JOURNAL_OTHER);
                        break;
                    case "p5":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PAPER_CORE_JOURNAL_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PAPER_CORE_JOURNAL_OTHER);
                        break;
                    default:
                }
                break;
            /*QC活动*/
            case 4:
                if (itemReport.equals("1")) {
                    scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.QC_PART_MAIN);
                    scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.QC_PART_OTHER);
                }
                switch (itemRewardClass) {
                    case "c1":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.QC_REWARD_CQ_FIRST_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.QC_REWARD_CQ_FIRST_OTHER);
                        break;
                    case "g1":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.QC_REWARD_CO_FIRST_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.QC_REWARD_CO_FIRST_OTHER);
                        break;
                    case "c2":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.QC_REWARD_CQ_SECOND_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.QC_REWARD_CQ_SECOND_OTHER);
                        break;
                    case "g2":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.QC_REWARD_CO_SECOND_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.QC_REWARD_CO_SECOND_OTHER);
                        break;
                    case "c3":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.QC_REWARD_CQ_THIRD_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.QC_REWARD_CQ_THIRD_OTHER);
                        break;
                    case "g3":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.QC_REWARD_CO_THIRD_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.QC_REWARD_CO_THIRD_OTHER);
                        break;
                    default:
                }
                break;
            /*科研项目*/
            case 5:
                switch (itemPublishClass) {
                    case "c1":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.RESEARCH_REWARD_CQ_FIRST_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.RESEARCH_REWARD_CQ_FIRST_OTHER);
                        break;
                    case "g1":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.RESEARCH_REWARD_CO_FIRST_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.RESEARCH_REWARD_CO_FIRST_OTHER);
                        break;
                    case "c2":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.RESEARCH_REWARD_CQ_SECOND_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.RESEARCH_REWARD_CQ_SECOND_OTHER);
                        break;
                    case "g2":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.RESEARCH_REWARD_CO_SECOND_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.RESEARCH_REWARD_CO_SECOND_OTHER);
                        break;
                    case "c3":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.RESEARCH_REWARD_CQ_THIRD_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.RESEARCH_REWARD_CQ_THIRD_OTHER);
                        break;
                    case "g3":
                        scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.RESEARCH_REWARD_CO_THIRD_MAIN);
                        scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.RESEARCH_REWARD_CO_THIRD_OTHER);
                        break;
                    default:
                }
                break;
            /*专利*/
            case 6:
                switch (patentType) {
                    case "p1":
                        if (entity.getItemStartDate() != null) {
                            setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PATENT_APPLY_UTILITY_MAIN);
                            setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PATENT_APPLY_UTILITY_OTHER);
                        }
                        if (entity.getItemPublishDate() != null) {
                            setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PATENT_GRANT_UTILITY_MAIN);
                            setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PATENT_GRANT_UTILITY_OTHER);
                        }
                        break;
                    case "p2":
                        if (entity.getItemStartDate() != null) {
                            scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PATENT_APPLY_INVENT_MAIN);
                            scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PATENT_APPLY_INVENT_OTHER);
                        }
                        if (entity.getItemPublishDate() != null) {
                            scores = setScoreForProject(scores, entity, mainStaffs, mainStaffEntities, ScoreDetial.PATENT_GRANT_INVENT_MAIN);
                            scores = setScoreForProject(scores, entity, otherStaffs, otherStaffEntities, ScoreDetial.PATENT_GRANT_INVENT_OTHER);
                        }

                        break;
                    default:
                }
                break;
            default:

        }
        Iterable<ScoringDetailEntity> scoringDetailEntities = scoringDao.saveAll(scores);
        return scoringDetailEntities;
    }

    /* *
     * @description findOne
     * @author yang tao
     * @date 2021-12-23 17:21
     * @Param: pkId:staff
     * @Param: pkId1:project
     * @return java.util.List<com.cqkjxy.entity.ScoringDetailEntity>
     */
    public List<ScoringDetailEntity> findOne(Integer pkId, Integer pkId1) {
        List<ScoringDetailEntity> entities = scoringDao.findOne(pkId, pkId1);
        return entities;
    }

    /* *
     * @description 根据员工（主要负责人，参与人员）设置分数
     * @author yang tao
     * @date 2021-12-03 16:30
     * @Param: entity：
     * @Param: staffs
     * @Param: staffEntities
     * @Param: s
     * @return void
     */
    private List<ScoringDetailEntity> setScoreForProject(List<ScoringDetailEntity> scores, TechProjectEntity entity, List<StaffEntity> staffs, Set<StaffEntity> staffEntities, float s) {
        if (!staffEntities.isEmpty()) {
            for (int i = 0; i < staffs.size(); i++) {
                ScoringDetailEntity score = new ScoringDetailEntity();
                StaffEntity next = staffs.get(i);
                score.setProjectEntity(entity);
                score.setStaffEntity(next);
                score.setStaffScore(s);
                scores.add(score);
                StaffEntity one = staffService.findOne(next.getPkId());
                if (one.getStaffScore() == null || one.getStaffScore() == 0) {
                    one.setStaffScore((double) s);
                } else {
                    one.setStaffScore(one.getStaffScore() + s);
                }
                staffService.save(one);
            }
        }
        return scores;
    }

    public List<ScoringDetailEntity> findByProject(int pkId) {

        List<ScoringDetailEntity> scores = scoringDao.findByProject(pkId);
        return scores;
    }
}
